package dongshi.daddy.encryptionAndDecryption.rsa;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import static dongshi.daddy.encryptionAndDecryption.rsa.RsaTest.RSA_ALGORITHM;

public class RsaUtil {
    //    private static byte[] privateKey;
    private static byte[] PRIVATE_KEY_BYTE;
    //    private static byte[] publicKey;
    private static byte[] PUBLIC_KEY_BYTE;

    static {
        try {
            // KeyPairGenerator类用于生成公钥和私钥对，基于RSA算法生成对象
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            // 初始化密钥对生成器，密钥大小为96-1024位
            keyPairGen.initialize(1024, new SecureRandom());
            // 生成一个密钥对，保存在keyPair中
            KeyPair keyPair = keyPairGen.generateKeyPair();
            RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();   // 得到私钥
            RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  // 得到公钥
            String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
            PUBLIC_KEY_BYTE = publicKey.getEncoded();
            // 得到私钥字符串
            String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
            PRIVATE_KEY_BYTE = privateKey.getEncoded();
            // 将公钥和私钥保存到Map
        /*keyMap.put(0,publicKeyString);  //0表示公钥
        keyMap.put(1,privateKeyString);  //1表示私钥*/
        /*PRIVATE_KEY = privateKeyString;
        PUBLIC_KEY = publicKeyString;*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 私钥加密
     *
     * @param data 待加密数据
     * @param key  密钥
     * @return byte[] 加密数据
     */
    public static byte[] encryptByPrivateKey(byte[] data/*, byte[] key*/) throws Exception {
//    public static String encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
        //取得私钥
//        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(PRIVATE_KEY_BYTE);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        //生成私钥
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        //数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(data);
//        return new String(cipher.doFinal(data));
//        return Base64.encode(cipher.doFinal(data));
    }

    public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
//    public static String encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
        //取得私钥
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
//        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(PRIVATE_KEY_BYTE);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        //生成私钥
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        //数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(data);
//        return new String(cipher.doFinal(data));
//        return Base64.encode(cipher.doFinal(data));
    }

    /**
     * 公钥解密
     *
     * @param data 待解密数据
     * @param key  密钥
     * @return byte[] 解密数据
     */
//    public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
    public static String decryptByPublicKey(byte[] data/*, byte[] key*/) throws Exception {
        //实例化密钥工厂
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        //初始化公钥
        //密钥材料转换
//        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(PUBLIC_KEY_BYTE);
        //产生公钥
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
        //数据解密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, pubKey);
        return new String(cipher.doFinal(data));
    }


    public static String decryptByPublicKey(byte[] data, byte[] key) throws Exception {
        //实例化密钥工厂
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        //初始化公钥
        //密钥材料转换
//        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
//        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(PUBLIC_KEY_BYTE);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
        //产生公钥
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
        //数据解密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, pubKey);
        return new String(cipher.doFinal(data));
    }

}
